package cn.iocoder.yudao.module.famous.doctor.service.reply;

import cn.iocoder.yudao.framework.mybatis.core.util.MyBatisUtils;
import cn.iocoder.yudao.module.famous.doctor.dal.dataobject.forumRecord.FamForumRecordDO;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.member.dal.dataobject.user.vo.MemberUserParamDO;
import cn.iocoder.yudao.module.member.service.user.MemberUserService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import java.util.*;

import cn.iocoder.yudao.module.famous.doctor.controller.admin.reply.vo.*;
import cn.iocoder.yudao.module.famous.doctor.dal.dataobject.reply.FamReplyDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.famous.doctor.convert.reply.FamReplyConvert;
import cn.iocoder.yudao.module.famous.doctor.dal.mysql.reply.FamReplyMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.famous.doctor.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 谈论会回复 Service 实现类
 *
 * @author Test1
 */
@Service
@Validated
public class FamReplyServiceImpl implements FamReplyService {

    @Resource
    private FamReplyMapper famReplyMapper;

    @Autowired
    RedisTemplate redisTemplate;
    @Resource
    private MemberUserService memberUserService;

    @Override
    public Long createFamReply(FamReplyCreateReqVO createReqVO) {
        // 插入
        FamReplyDO famReply = FamReplyConvert.INSTANCE.convert(createReqVO);
        famReplyMapper.insert(famReply);
        // 返回
        return famReply.getId();
    }

    @Override
    public void updateFamReply(FamReplyUpdateReqVO updateReqVO) {
        // 校验存在
        validateFamReplyExists(updateReqVO.getId());
        // 更新
        FamReplyDO updateObj = FamReplyConvert.INSTANCE.convert(updateReqVO);
        famReplyMapper.updateById(updateObj);
    }

    @Override
    public void deleteFamReply(Long id) {
        // 校验存在
        validateFamReplyExists(id);
        // 删除
        famReplyMapper.deleteById(id);
    }

    private void validateFamReplyExists(Long id) {
        if (famReplyMapper.selectById(id) == null) {
            throw exception(FAM_REPLY_NOT_EXISTS);
        }
    }

    @Override
    public FamReplyDO getFamReply(Long id) {
        return famReplyMapper.selectById(id);
    }

    @Override
    public List<FamReplyDO> getFamReplyList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return famReplyMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<FamReplyDO> getFamReplyPage(FamReplyPageReqVO pageReqVO) {
        return famReplyMapper.selectPage(pageReqVO);
    }

    @Override
    public List<FamReplyDO> getFamReplyList(FamReplyExportReqVO exportReqVO) {
        return famReplyMapper.selectList(exportReqVO);
    }

    @Override
    public IPage<FamReplyRespTwoVO> getFamReplyPageParticulars(FamReplyPageTwoReqVO pageVO) {
        IPage<FamReplyRespTwoVO> famReplyRespTwoVOIPage = famReplyMapper.selectPage2(MyBatisUtils.buildPage(pageVO), pageVO);
        String key = "fam_forum_record" + ":" + 1;
        List<FamForumRecordDO> values = redisTemplate.opsForHash().values(key);
        famReplyRespTwoVOIPage.getRecords()
                .stream()
                .forEach(famReplyRespTwoVO -> {
                    MemberUserDO user = memberUserService.getUser(famReplyRespTwoVO.getReplyUser());
                    if (user!=null) {
                        MemberUserParamDO memberUserParamDO = new MemberUserParamDO();
                        BeanUtils.copyProperties(user,memberUserParamDO);
                        famReplyRespTwoVO.setPublishUserMessage(memberUserParamDO);
                    }
                    long count = values.stream()
                            .filter(record -> record.getThumbsupId() == famReplyRespTwoVO.getToreplyId())
                            .count();
                    famReplyRespTwoVO.setCollectCount(count);
                });

        return famReplyRespTwoVOIPage;
    }
}
